package hud;

import com.jme.image.Texture;
import com.jme.image.Texture.WrapMode;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

public class StatusScreen extends Quad {

	private static final long serialVersionUID = 1L;
	protected TextureState tstate;
	protected int width, height;
	protected Quad health;
	protected Quad mana;
	protected Quad rage;
	protected Quad ammo;
	protected int hpmprWidth;
	protected int aWidth;
	protected Text nick;

	public StatusScreen() {
		super("Status", 1, 1);
	}

	/**
	 * this method must be called after beeing attached to the scene
	 * 
	 * @param name
	 *            - the nick of the player
	 */
	public void setup(String name) {
		setImage("res/hud/hud.png");
		setLocalTranslation(width / 2, DisplaySystem.getDisplaySystem()
				.getHeight()
				- height / 2, 0);
		setupBars();
		setupNick(name);
	}

	protected void setImage(String image) {
		// load the image and apply it as a texture
		TextureState ts = DisplaySystem.getDisplaySystem().getRenderer()
				.createTextureState();
		ts.setEnabled(true);
		ts.setTexture(TextureManager.loadTexture(getClass().getClassLoader()
				.getResource(image), Texture.MinificationFilter.Trilinear,
				Texture.MagnificationFilter.Bilinear));
		setRenderState(ts);

		width = ts.getTexture().getImage().getWidth();
		height = ts.getTexture().getImage().getHeight();
		resize(width, height);

		// make it transparent
		BlendState as = DisplaySystem.getDisplaySystem().getRenderer()
				.createBlendState();
		as.setEnabled(true);
		as.setBlendEnabled(true);
		as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		as
				.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
		setRenderState(as);

		// turn light off
		setLightCombineMode(Spatial.LightCombineMode.Off);

		updateRenderState();
	}

	protected void setupBars() {
		// load the emptyBar Image and apply it as a texture;
		String image = "res/hud/emptyBar.png";
		TextureState ts = DisplaySystem.getDisplaySystem().getRenderer()
				.createTextureState();
		ts.setEnabled(true);
		Texture tex = TextureManager.loadTexture(getClass().getClassLoader()
				.getResource(image), Texture.MinificationFilter.Trilinear,
				Texture.MagnificationFilter.Bilinear);
		tex.setWrap(WrapMode.Repeat);
		ts.setTexture(tex);

		// make it transparent
		BlendState as = DisplaySystem.getDisplaySystem().getRenderer()
				.createBlendState();
		as.setEnabled(true);
		as.setBlendEnabled(true);
		as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		as
				.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);

		int width = ts.getTexture().getImage().getWidth();
		hpmprWidth = width;
		int height = ts.getTexture().getImage().getHeight();

		Vector2f topLeft = new Vector2f(getLocalTranslation().x - this.width
				/ 2 + width / 2, getLocalTranslation().y + this.height / 2
				- height / 2);

		health = new Quad("HealthBar", width, height);
		health.setRenderState(ts);
		health.setRenderState(as);
		health.setLocalTranslation(topLeft.x + 23, topLeft.y - 24, 0);
		health.setLightCombineMode(Spatial.LightCombineMode.Off);
		health.updateRenderState();

		mana = new Quad("ManaBar", width, height);
		mana.setRenderState(ts);
		mana.setRenderState(as);
		mana.setLocalTranslation(topLeft.x + 23, topLeft.y - 44, 0);
		mana.setLightCombineMode(Spatial.LightCombineMode.Off);
		mana.updateRenderState();

		rage = new Quad("RageBar", width, height);
		rage.setRenderState(ts);
		rage.setRenderState(as);
		rage.setLocalTranslation(topLeft.x + 23, topLeft.y - 61, 0);
		rage.setLightCombineMode(Spatial.LightCombineMode.Off);
		rage.updateRenderState();

		// load the emptyAmmo Image and apply it as a texture;
		image = "res/hud/emptyAmmo.png";
		ts = DisplaySystem.getDisplaySystem().getRenderer()
				.createTextureState();
		ts.setEnabled(true);
		tex = TextureManager.loadTexture(getClass().getClassLoader()
				.getResource(image), Texture.MinificationFilter.Trilinear,
				Texture.MagnificationFilter.Bilinear);
		tex.setWrap(WrapMode.Repeat);
		ts.setTexture(tex);

		width = ts.getTexture().getImage().getWidth();
		aWidth = width;
		height = ts.getTexture().getImage().getHeight();

		topLeft = new Vector2f(getLocalTranslation().x - this.width / 2 + width
				/ 2, getLocalTranslation().y + this.height / 2 - height / 2);

		ammo = new Quad("AmmoBar", width, height);
		ammo.setRenderState(ts);
		ammo.setRenderState(as);
		ammo.setLocalTranslation(topLeft.x + 23, topLeft.y - 75, 0);
		ammo.setLightCombineMode(Spatial.LightCombineMode.Off);
		ammo.updateRenderState();
	}

	protected void setupNick(String name) {
		nick = Text.createDefaultTextLabel("player1ScoreText", name);
		nick.setLightCombineMode(Spatial.LightCombineMode.Off);

		Vector2f topLeft = new Vector2f(getLocalTranslation().x - this.width
				/ 2, getLocalTranslation().y + this.height / 2);
		nick.setRenderQueueMode(Renderer.QUEUE_ORTHO);

		nick
				.setLocalTranslation(new Vector3f(topLeft.x + 5,
						topLeft.y - 20, 1));

		getParent().attachChild(nick);

		getParent().updateRenderState();
	}

	/**
	 * @param name
	 *            - New Nickname
	 */
	public void setNick(String name) {
		nick.getText().replace(0, nick.getText().length(), name);
	}

	/**
	 * @param hp
	 *            - Health in %
	 * @param mp
	 *            - Mana in %
	 * @param r
	 *            - Rage in %
	 * @param a
	 *            - Ammo in %
	 */
	public void update(float hp, float mp, float r, float a) {
		hp = (hp - 1) * -1;
		mp = (mp - 1) * -1;
		r = (r - 1) * -1;
		a = (a - 1) * -1;
		health.resize(hpmprWidth * hp, health.getHeight());
		health.setLocalTranslation(23 + hpmprWidth - hpmprWidth / 2 * hp,
				health.getLocalTranslation().y, 0);
		mana.resize(hpmprWidth * mp, mana.getHeight());
		mana.setLocalTranslation(23 + hpmprWidth - hpmprWidth / 2 * mp, mana
				.getLocalTranslation().y, 0);
		rage.resize(hpmprWidth * r, rage.getHeight());
		rage.setLocalTranslation(23 + hpmprWidth - hpmprWidth / 2 * r, rage
				.getLocalTranslation().y, 0);
		ammo.resize(aWidth * a, ammo.getHeight());
		ammo.setLocalTranslation(23 + aWidth - aWidth / 2 * a, ammo
				.getLocalTranslation().y, 0);
	}

	public void draw(Renderer r) {
		// r.setOrtho();
		super.draw(r);
		health.draw(r);
		mana.draw(r);
		rage.draw(r);
		ammo.draw(r);
		// r.unsetOrtho();
	}

}